package com.legoo.studaychineseonline.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.legoo.studaychineseonline.R;
import com.legoo.studaychineseonline.adapter.ClassFragmentAdapter;
import com.legoo.studaychineseonline.adapter.ClassFragmentTimeAdapter;
import com.legoo.studaychineseonline.base.BaseFragment;
import com.legoo.studaychineseonline.bean.KeChengBiaoBean;
import com.legoo.studaychineseonline.http.API;
import com.legoo.studaychineseonline.http.XUtil;
import com.legoo.studaychineseonline.view.MyCommonPopupWindow;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.format.ArrayWeekDayFormatter;
import com.prolificinteractive.materialcalendarview.format.MonthArrayTitleFormatter;

import org.threeten.bp.LocalDate;
import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ClassFragment extends BaseFragment implements MyCommonPopupWindow.ViewInterface, OnDateSelectedListener {
    @BindView(R.id.v_zhan)
    View vZhan;
    @BindView(R.id.tv_title_left)
    TextView tvTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.iv_time)
    ImageView ivTime;
    Unbinder unbinder;

    @BindView(R.id.rv_class)
    RecyclerView rvClass;
    Unbinder unbinder1;
    @BindView(R.id.rv_time)
    RecyclerView rvTime;
    Unbinder unbinder2;
    private MyCommonPopupWindow popupWindow;
    List<String> list = new ArrayList<>();
    List<String> list2 = new ArrayList<>();

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        unbinder = ButterKnife.bind(this, view);
        tvTitleLeft.setVisibility(View.INVISIBLE);
        tvTitleRight.setVisibility(View.INVISIBLE);
        tvTitle.setText("我的课程");
        list.add("0");
        list.add("0");
        list.add("0");
        rvClass.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvTime.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        ClassFragmentAdapter classFragmentAdapter = new ClassFragmentAdapter(R.layout.item_activity, list);
        rvClass.setAdapter(classFragmentAdapter);
        for (int i=0;i<14;i++){
            list2.add("0");
        }
        ClassFragmentTimeAdapter classFragmentTimeAdapter  = new ClassFragmentTimeAdapter(R.layout.item_class_fragment_time,list2);
        rvTime.setAdapter(classFragmentTimeAdapter);
        rvTime.scrollToPosition(7);
        getKeChengBiao("");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_class;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    //时间弹出框
    public void timePopWindows(View view) {
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        popupWindow = new MyCommonPopupWindow.Builder(context)
                .setView(R.layout.popup_time)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setAnimationStyle(R.style.AnimDown)
                .setBackGroundLevel(0.9f)
                .setViewOnclickListener(this)
                .setOutsideTouchable(true)
                .create();
        popupWindow.showAsDropDown(view);
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            case R.layout.popup_time:
                MaterialCalendarView timeView = view.findViewById(R.id.calendarView);
                //英文月
                timeView.setTitleFormatter(new MonthArrayTitleFormatter(getResources().getTextArray(R.array.custom_months)));
                //英文周
                timeView.setWeekDayFormatter(new ArrayWeekDayFormatter(getResources().getTextArray(R.array.custom_weekdays)));
                //中午月
                timeView.setTitleFormatter(null);
                //中午周
                timeView.setWeekDayFormatter(null);

                timeView.setOnDateChangedListener(this);
                final LocalDate calendar = LocalDate.now();
                //设置最小日期
                //     timeView.state().edit().setMinimumDate(calendar).commit();
                timeView.setSelectedDate(calendar);
                break;
            default:
        }
    }


    //日期选择回调
    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
        popupWindow.dismiss();
    }


    @OnClick({R.id.iv_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_time:
                timePopWindows(ivTime);
                break;
            default:
        }
    }

    private void getKeChengBiao(String date) {
        Map<String, Object> map = new HashMap<>();
        map.put("access_token", "7a919a8a-502f-11e9-9b63-309c23d6f138");
        map.put("post_type", "date");
        map.put("date", " 2019-04-02 ");

        XUtil.Post(API.CURRICULUM, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                KeChengBiaoBean keChengBiaoBean = new Gson().fromJson(result, KeChengBiaoBean.class);
                Log.e("KeChengBiaoBean", keChengBiaoBean.toString());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder2 = ButterKnife.bind(this, rootView);
        return rootView;
    }
}
